import sys, requests
import time
from PyQt6.QtWidgets import (
    QApplication,
    QMainWindow,
    QLabel,
    QLineEdit,
    QPushButton,
    QTextEdit,
    QVBoxLayout,
    QWidget,
    QFileDialog,
    QStatusBar,
)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("课程内容总结助手")
        self.setGeometry(100, 100, 600, 400)

        # 创建主窗口部件
        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)

        # 创建布局
        self.layout = QVBoxLayout(self.central_widget)

        # 创建课程名称标签和输入框
        self.course_name_label = QLabel("预设文件名:")
        self.course_name_input = QLineEdit()

        # 创建音频文字选择文件按钮
        self.audio_text_label = QLabel("音频文件:")
        self.select_file_button = QPushButton("选择文件")
        self.select_file_button.clicked.connect(self.select_file)

        # 创建生成素材按钮
        self.generate_button = QPushButton("生成课堂总结")
        self.generate_button.clicked.connect(self.generate_material)

        # 创建文本显示区域
        self.text_display = QTextEdit()
        self.text_display.setReadOnly(True)

        # 创建状态栏
        self.status_bar = QStatusBar()
        self.setStatusBar(self.status_bar)

        # 添加部件到布局
        self.layout.addWidget(self.course_name_label)
        self.layout.addWidget(self.course_name_input)
        self.layout.addWidget(self.audio_text_label)
        self.layout.addWidget(self.select_file_button)
        self.layout.addWidget(self.generate_button)
        self.layout.addWidget(self.text_display)

        # 初始化文件路径
        self.file_path = ""

    def select_file(self):
        file_dialog = QFileDialog()
        self.file_path, _ = file_dialog.getOpenFileName(
            self, "选择文件", "", "Text Files (*.mp3);;All Files (*)"
        )
        if self.file_path:
            self.status_bar.showMessage(f"已选择文件: {self.file_path}")
        else:
            self.status_bar.showMessage("请上传有效文件")

    def generate_material(self):
        if not self.file_path:
            self.status_bar.showMessage("请先选择文件")
            return

        try:
            with open(self.file_path, "rb") as f:
                # print(self.file_path)
                url = "http://120.25.106.154:8000/uploadfile/"
                files = {"file": (self.file_path, f, "audio/mpeg")}
                time1 = time.time()

                response = requests.post(url, files=files, timeou=600)
                print(f"上传文件耗时：{int(time.time()-time1)}s")
                print(response.status_code)
                print(response.text)

                processed_content = response.json()["content"]

                # 显示处理后的内容
                self.text_display.setText(processed_content)

                # 获取预设文件名
                preset_filename = self.course_name_input.text().strip()
                if not preset_filename:
                    self.status_bar.showMessage("请填写预设文件名")
                    return

                # 保存处理后的内容到新的txt文件
                output_file_path = f"{preset_filename}.txt"
                with open(output_file_path, "w", encoding="utf-8") as output_file:
                    output_file.write(processed_content)

                self.status_bar.showMessage(
                    f"课堂总结生成成功，已保存为: {output_file_path}"
                )
        except Exception as e:
            self.status_bar.showMessage(f"文件读取失败: {e}")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    # time.sleep(300)
    sys.exit(app.exec())
